/**
 * @filename:WarehouseController Jan 14, 2025
 * @project Wonder Vue Admin Api  1.1.0
 * Copyright(c) 2020 Wonder Co. Ltd. 
 * All right reserved. 
 */
package com.wonder.app.controller;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Date;
import java.util.Map;

import com.wonder.admin.domain.SysAdmin;
import com.wonder.admin.service.SysAdminService;
import com.wonder.app.domain.Warehouse;
import com.wonder.app.service.WarehouseService;
import com.wonder.app.request.WarehouseRequest;
import com.wonder.framework.authorization.CheckAdminToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.wonder.common.common.AjaxMap;
import com.wonder.common.common.AjaxResult;
import com.wonder.common.common.CommonPage;
import com.wonder.common.utils.MessageUtils;
import com.wonder.common.utils.StringUtils;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParam;

import javax.servlet.http.HttpServletRequest;

/**
 * 
 * <p>说明： 仓库列表API接口层</P>
 * @version: 1.1.0
 * @author: Wonder
 * @time    Jan 14, 2025
 *
 */
@Api(tags = "仓库列表")
@RestController
@RequestMapping("/app/Warehouse")
public class WarehouseController {
	
	@Autowired
	private WarehouseService warehouseService;

	@Autowired
	private SysAdminService adminService;

	@Autowired
	private CheckAdminToken checkAdminToken;
	
	@ApiOperation(value = "仓库列表列表")
	@RequestMapping(value = "/index", method = RequestMethod.POST)
	public Map<String,Object> index(@RequestBody WarehouseRequest warehouseRequest) {
		Page<Warehouse> page = new Page<Warehouse>(warehouseRequest.getPage(), warehouseRequest.getLimit());
		IPage<Warehouse> list = warehouseService.selectList(warehouseRequest, page);
		
		Map<String,Object> retMap=new HashMap<>();
		retMap.put("data",CommonPage.restPage(list));
		
  
		return AjaxMap.success(retMap);
	}
	
	/**
	 * @explain 修改排序开关
	 * @param   entity
	 * @return  Boolean
	 * @author  Wonder
	 */
	@PostMapping("/updateExt")
	@ApiOperation(value = "修改排序开关", notes = "作者：Wonder")
	public AjaxResult<Warehouse> updateExt(@RequestBody Warehouse entity){
		if (null!=entity) {
			boolean rsg = warehouseService.updateById(entity);
			if (rsg) {
				  return AjaxResult.success("system.updateSuccess");
			  }else {
				  return AjaxResult.failed("system.updateFailed");
			  }
		}else {
			return AjaxResult.failed("system.parameterError");
		}
	}
	
	/**
	 * @explain 添加
	 * @param   entity
	 * @return  Boolean
	 * @author  Wonder
	 */
	@PostMapping("/add")
	@ApiOperation(value = "添加", notes = "作者：Wonder")
	public AjaxResult<Warehouse> add(@RequestBody Warehouse entity){

		if (null!=entity) {
		    
		    String validate=warehouseService.validate(entity,"new");
		    if(StringUtils.hasText(validate))
		         return AjaxResult.failed(validate);    
			boolean rsg = warehouseService.save(entity);
			if (rsg) {
				  return AjaxResult.success("system.insertSuccess");
			  }else {
				  return AjaxResult.failed("system.insertFailed");
			  }
		}else {
			return AjaxResult.failed("system.parameterError");
		}
	}
	
	/**
	 * @explain 查询对象  <swagger GET请求>
	 * @param   map 对象参数：id
	 * @return  JsonResult
	 * @author  Wonder
	 */
	@PostMapping("/getUpdateInfo")
	@ApiOperation(value = "获取仓库列表对象", notes = "作者：Wonder")
	public AjaxResult<Warehouse> getUpdateInfo(@RequestBody Map<String,Object> map){
	    if(map==null) return AjaxResult.failed("system.parameterError");
//		 Warehouse warehouse=warehouseService.getById(id);
//
//		if (null!=warehouse ) {
//			 return AjaxResult.success(warehouse);
//		}else {
//			 return AjaxResult.failed("system.parameterError");
//		}
		return null;
	}
	
	/**
	 * @explain 修改
	 * @param   entity
	 * @return  Boolean
	 * @author  Wonder
	 */
	@PostMapping("/update")
	@ApiOperation(value = "修改", notes = "作者：Wonder")
	public AjaxResult<Warehouse> update(@RequestBody Warehouse entity){
		if (null!=entity) {
		    String validate=warehouseService.validate(entity,"update");
		    if(StringUtils.hasText(validate))
		         return AjaxResult.failed(validate);
			boolean rsg = warehouseService.updateById(entity);
			if (rsg) {
				  return AjaxResult.success("system.updateSuccess");
			  }else {
				  return AjaxResult.failed("system.updateFailed");
			  }
		}else {
			return AjaxResult.failed("system.parameterError");
		}
	}
	
	/**
	 * @explain 删除
	 * @param   map 对象参数：id
	 * @return  JsonResult
	 * @author  Wonder
	 */
	@PostMapping("/delete")
	@ApiOperation(value = "删除", notes = "作者：Wonder")
	public AjaxResult<Warehouse> delete(@RequestBody Map<String,Object> map){
		if(map==null)return AjaxResult.failed("system.parameterError");
		String ids=map.get("id").toString();
		ids=StringUtils.remove(ids, '[');
		ids=StringUtils.remove(ids, ']');
		String[] deleteIds=StringUtils.split(ids, ',');
		List<Long> idList=new ArrayList<>(); 
		for(int i=0;i<deleteIds.length;i++)
		{
			idList.add( Long.parseLong(deleteIds[i].trim()));//整数数组
		}

	    boolean rsg = warehouseService.removeBatchByIds(idList);
	    if (rsg) {
		    return AjaxResult.success("system.deleteSuccess");
	    }else {
		   return AjaxResult.failed("system.deleteFailed");
	    }
		
	}
	
	/**
	 * @explain 查看对象明细  
	 * @param   map 对象参数：id
	 * @return  JsonResult
	 * @author  Wonder
	 */
	@PostMapping("/detail")
	@ApiOperation(value = "获取仓库列表对象明细", notes = "作者：Wonder")
	public AjaxResult<Warehouse> detail(@RequestBody Map<String,Object> map){
	    if(map==null) return AjaxResult.failed("system.parameterError");
//		Warehouse warehouse=warehouseService.getById(id);
//		if (null!=warehouse ) {
//			 return AjaxResult.success(warehouse);
//		}else {
//			 return AjaxResult.failed("system.parameterError");
//		}
		return null;
	}

	/**
	 * @explain 判断仓库是否启用仓位
	 * @param   map 对象参数：id
	 * @return  JsonResult
	 * @author  Wonder
	 */
	@PostMapping("/check_stock")
	@ApiOperation(value = "获取仓库列表对象明细", notes = "作者：Wonder")
	public AjaxResult<?> checkStock(@RequestBody Map<String,Object> map) throws Exception{

		Boolean res = warehouseService.checkStock(map);

		return AjaxResult.success(res);


	}


	/**
	 * @explain 根据仓库获取仓位
	 * @param   map 对象参数：id
	 * @return  JsonResult
	 * @author  Wonder
	 */
	@PostMapping("/get_location")
	@ApiOperation(value = "获取仓库列表对象明细", notes = "作者：Wonder")
	public AjaxResult<?> getLocation(@RequestBody Map<String,Object> map) throws Exception{

		List<String> dataList = warehouseService.getLocation(map);
		if (null != dataList ) {
			return AjaxResult.success(dataList);
		}else {
			return AjaxResult.failed("system.parameterError");
		}

	}

	/**
	 * @explain 查询所有仓库(从金蝶查询)
	 * @param   map 对象参数：id
	 * @return  JsonResult
	 * @author  Wonder
	 */
	@PostMapping("/get_stock_list")
	@ApiOperation(value = "查询所有仓库(从金蝶查询)", notes = "作者：Wonder")
	public AjaxResult<?> getStockSist(HttpServletRequest request, @RequestBody Map<String,Object> map) throws Exception{
		String token = checkAdminToken.getTokenFormRequest(request);
		SysAdmin user = adminService.getAdminByToken(token);
		String userName = user.getName();
		Long user_id = user.getUser_id();
		Map<String,Object> dataList = warehouseService.getStockList(map,user_id);
		if (null != dataList ) {
			return AjaxResult.success(dataList);
		}else {
			return AjaxResult.failed("system.parameterError");
		}

	}

	/**
	 * @explain 查询所有仓库(从金蝶查询)
	 * @param   map 对象参数：id
	 * @return  JsonResult
	 * @author  Wonder
	 */
	@PostMapping("/get_house_list")
	@ApiOperation(value = "查询所有车间(从金蝶查询)", notes = "作者：Wonder")
	public AjaxResult<?> getHosutList(HttpServletRequest request, @RequestBody Map<String,Object> map) throws Exception{
		String token = checkAdminToken.getTokenFormRequest(request);
		SysAdmin user = adminService.getAdminByToken(token);
		String userName = user.getName();
		Long user_id = user.getUser_id();
		Map<String,Object> dataList = warehouseService.getHosutList(map,user_id);
		if (null != dataList ) {
			return AjaxResult.success(dataList);
		}else {
			return AjaxResult.failed("system.parameterError");
		}

	}

	/**
	 * @explain 查询所有仓库(从金蝶查询)
	 * @param   map 对象参数：id
	 * @return  JsonResult
	 * @author  Wonder
	 */
	@PostMapping("/get_stock_list_by_user")
	@ApiOperation(value = "查询当前用户所绑定的仓库", notes = "作者：Wonder")
	public AjaxResult<?> getStockListByUser(HttpServletRequest request, @RequestBody Map<String,Object> map) throws Exception{
		String token = checkAdminToken.getTokenFormRequest(request);
		SysAdmin user = adminService.getAdminByToken(token);
		String userName = user.getName();
		Long user_id = user.getUser_id();
		Map<String,Object> dataList = warehouseService.getStockListByUser(map,user_id);
		if (null != dataList ) {
			return AjaxResult.success(dataList);
		}else {
			return AjaxResult.failed("system.parameterError");
		}

	}


}